local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__zhiqing",
  tags = { LB_U.LeaderSkill },
  max_branches_use_time = function(self, player)
    local room = Fk:currentRoom()
    local list = {}
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryRound] = 1 }
    end
    return list
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__zhiqing"] = "炙擎",
  [":lb_zero__zhiqing"] = "<b><a href='lb_leader_skill'><font color='red'>领袖技</font></a></b>，“卡吕冬之子”角色计算与其他角色的距离-1，且造成的伤害改为火焰伤害。每轮每名角色限一次，“卡吕冬之子”角色需要响应伤害牌时，同势力角色可打出一张同名牌以视为响应之。",

  ["@[skill_players]lb_zero__zhiqing"] = "炙擎",
  
  ["#lb_zero__zhiqing_ask"] = "炙擎：你可以打出一张 %arg 为 %dest 响应 %arg2",
  ["#lb_zero__zhiqing_choose_slash"] = "炙擎：请视为使用一张【杀】，用以响应 %arg2",
  --["#lb_zero__zhiqing_choose_jink"] = "炙擎：你可以打出一张 %arg，视为 %dest 使用一张【闪】，用以响应 %arg",

  ["$lb_zero__zhiqing1"] = "胜利属于勇者。",
  ["$lb_zero__zhiqing2"] = "要打架吗？带上我。",
}
local zhiqing_spec = {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and LB_U.CompareKingdomWith(target, "lb_zero_kalvdongzhizi")
        and (data.extraData == nil or data.extraData.lb_zero__zhiqing_ask == nil)
        and data.eventData and data.eventData.card and data.eventData.card.is_damage_card
        and Exppattern:Parse(data.pattern):matchExp("slash,jink")
        and skel:withinBranchTimesLimit(player, tostring(target.id), Player.HistoryRound)
        and (event ~= fk.AskForCardUse
          or not Exppattern:Parse(data.pattern):matchExp("slash")
          or table.find(player.room.alive_players, function(to)
            return target:canUseTo(Fk:cloneCard("slash"), to, data.extraData)
          end)) -- 没合法目标不询问
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local card = data.eventData.card
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:isAlive() and LB_U.CompareKingdomWith(p, "lb_zero_kalvdongzhizi") then
        local params = { ---@type AskToUseCardParams
          skill_name = skel.name,
          pattern = card.trueName,
          prompt = "#lb_zero__zhiqing_ask::" .. target.id .. ":" .. card.trueName .. ":" .. card:toLogString(),
          cancelable = true,
          extra_data = { lb_zero__zhiqing_ask = true }
        }
        local respond = room:askToResponse(p, params)
        if respond then
          event:setCostData(self, { respond = respond })
          room:useSkill(player, self, function()
            self:use(event, target, player, data)
          end, event.skill_data)
          break
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, tostring(target.id))
    local room = player.room
    local respond = event:getCostData(self).respond
    room:responseCard(respond)
    if target.dead then return end
    local name = "slash"
    if Exppattern:Parse(data.pattern):matchExp("slash") then
      if event == fk.AskForCardUse then
        local use = room:askToUseVirtualCard(target, {
          name = "slash",
          skill_name = skel.name,
          prompt = "#lb_zero__zhiqing_choose_slash:::" .. data.eventData.card:toLogString(),
          cancelable = false,
          skip = true,
          extra_data = data.extraData,
          event_data = data.eventData,
        })
        if use then
          data.result = use
          return true
        end
      end
    else
      name = "jink"
    end
    data.result = {
      from = target,
      card = Fk:cloneCard(name),
      tos = {},
    }
    return true
  end,
}

skel:addEffect(fk.AskForCardUse, zhiqing_spec, { check_skill_limit = true })
skel:addEffect(fk.AskForCardResponse, zhiqing_spec, { check_skill_limit = true })

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__zhiqing",
    { name = skel.name, scope = Player.HistoryRound, players = table.map(player.room.players, Util.IdMapper) })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__zhiqing", 0)
end)

return skel
